<?php
class Habit_reward extends MY_Controller
{	
	function __construct() {
        parent::__construct();
    }

    function add(){
    	$data = $this->user_post_data;
    	if(empty($data['task_id'])){
    		echo json_encode(array("code" => 0, "msg" => "缺少习惯"));
    		exit;
    	}
    	$task_info = get_field_by_where("tasks", "*", "id='{$data['task_id']}'");
    	if(!$task_info){
    		echo json_encode(array("code" => 0, "msg" => "找不到习惯"));
    		exit;
    	}
    	if($task_info['user_id'] != $data['user_token']['user_id']){
    		echo json_encode(array("code" => 0, "msg" => "没有权限设置激励"));
    		exit;
    	}
    	$start_time = strtotime($data['start_time']);
    	$end_time = strtotime($data['end_time']);
    	$days = count_days($end_time, $start_time) + 1;
    	$reward = array(
            'task_id' 	=> $data['task_id'], 
            'child_id' 	=> $data['user_token']['child_id'],
            'user_id' 	=> $data['user_token']['user_id'], 
            'days' 		=> $days, 
            'reward' 	=> $data['reward_text'],
            'start_date'=> $data['start_time'],
            'end_date'  => $data['end_time'],
            'add_time' 	=> time(), 
        );
        $this->db->insert('tiku_habit_reward',$reward);
        echo json_encode(array("code" => 1));
    }
}